shithub: pokered

Download patch

ref: 723e42497962f4838947b5ff6c4bda3198ae2d52
parent: 4b28ddb11f38ad2a45b2d7c6cb4c93c2a539ec71
author: sawakita <sawakitanoodles@gmail.com>
date: Sun Sep 30 16:10:13 EDT 2012

Make analyze_incbins dirs reference os-independent

This should fix a bug caused by comparing string literals to
directories. It should also be safer, using "os" module to access paths.

--- a/extras/analyze_incbins.py
+++ b/extras/analyze_incbins.py
@@ -7,6 +7,7 @@
 import subprocess
 import json
 from extract_maps import rom, assert_rom, load_rom, calculate_pointer, load_map_pointers, read_all_map_headers, map_headers
+from pokered_dir import pokered_dir
 
 try:
     from pretty_map_headers import map_header_pretty_printer, map_name_cleaner
@@ -26,24 +27,17 @@
     if type(offset) == str: offset = int(offset, base)
     return int(offset) % 0x4000 + 0x4000
 
-def load_asm(filename="../main.asm"):
+def load_asm(filename=os.path.join(pokered_dir, "main.asm")):
     """loads the asm source code into memory
     this also detects if the revision of the repository
     is using main.asm, common.asm or pokered.asm, which is
     useful when generating images in romvisualizer.py"""
     global asm
-    defaults = ["../main.asm", "../common.asm", "../pokered.asm"]
+    defaults = [os.path.join(pokered_dir, f) for f in ["main.asm", "common.asm", "pokered.asm"]]
     if filename in defaults:
-        if os.path.exists("../main.asm"):
-            asm = open("../main.asm", "r").read().split("\n")
-        elif os.path.exists("../common.asm"):
-            asm = open("../common.asm", "r").read().split("\n")
-        elif os.path.exists("../pokered.asm"):
-            asm = open("../pokered.asm", "r").read().split("\n")
-        else:
-            raise "this shouldn't happen"
-    else:
         asm = open(filename, "r").read().split("\n")
+    else:
+        raise Exception("this shouldn't happen")
     return asm
 
 def isolate_incbins():
@@ -168,7 +162,9 @@
     newfile_fh.close()
 
     try:
-        diffcontent = subprocess.check_output("diff -u ../main.asm " + newfile_filename, shell=True)
+        diffcontent = subprocess.check_output(
+                "diff -u {0} {1}".format(os.path.join(pokered_dir, "main.asm"), newfile_filename),
+                shell=True)
     except AttributeError, exc:
         raise exc
     except Exception, exc:
@@ -208,7 +204,7 @@
     fh.close()
 
     #apply the patch
-    os.system("patch ../main.asm temp.patch")
+    os.system("patch {0} temp.patch".format(os.path.join(pokered_dir, "main.asm")))
     
     #remove the patch
     os.system("rm temp.patch")
@@ -243,8 +239,12 @@
     fh.close()
 
     #apply the patch
-    os.system("cp ../main.asm ../main1.asm")
-    os.system("patch ../main.asm temp.patch")
+    os.system("cp {0} {1}".format(
+        os.path.join(pokered_dir, "main.asm"),
+        os.path.join(pokered_dir, "main1.asm")))
+    os.system("patch {0} {1}".format(
+        os.path.join(pokered_dir, "main.asm"),
+        "temp.patch"))
 
     #remove the patch
     os.system("rm temp.patch")
@@ -252,11 +252,13 @@
     #confirm it's working
     if do_compile:
         try:
-            subprocess.check_call("cd ../; make clean; LC_CTYPE=C make", shell=True)
+            subprocess.check_call("cd {0}; make clean; LC_CTYPE=C make".format(pokered_dir), shell=True)
             return True
         except Exception, exc:
             if try_fixing:
-                os.system("mv ../main1.asm ../main.asm")
+                os.system("mv {0} {1}".format(
+                    os.path.join(pokered_dir, "main1.asm"),
+                    os.path.join(pokered_dir, "main.asm")))
             return False
 
 def index(seq, f):
--- /dev/null
+++ b/extras/pokered_dir.py
@@ -1,0 +1,4 @@
+import os
+
+#main dir of repo (simply one level up than here)
+pokered_dir = os.path.dirname(os.path.dirname(__file__))