From 91fdf34a6bd2d224de12dafb8b28c339bb2731a2 Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Wed, 28 Nov 2018 01:53:02 +0300 Subject: [PATCH] Normalize build.py according to pep8, Fix .editorconfig for python indentation https://github.com/iitc-project/ingress-intel-total-conversion/pull/861 https://github.com/iitc-project/ingress-intel-total-conversion/pull/1263 #2 --- .editorconfig | 7 +++- build.py | 94 +++++++++++++++++++++++------------------------- buildsettings.py | 5 ++- setup.cfg | 5 +++ 4 files changed, 57 insertions(+), 54 deletions(-) create mode 100644 setup.cfg diff --git a/.editorconfig b/.editorconfig index 3bfaabb2b..564d025a2 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,9 +6,14 @@ root = true [*.{js,py}] charset = utf-8 indent_style = space -indent_size = 2 trim_trailing_whitespace = true +[*.js] +indent_size = 2 + +[*.py] +indent_size = 4 + # Following JS code style rules do not have EditorConfig support yet: # - Use identity operators (===) over equality operators (==). # - Opening brace at end of line. e.g. "if(a) {", "} else {", "} else if (b) }" diff --git a/build.py b/build.py index 75aed2413..57cc968aa 100755 --- a/build.py +++ b/build.py @@ -13,9 +13,9 @@ import hashlib try: - import urllib2 + import urllib2 except ImportError: - import urllib.request as urllib2 + import urllib.request as urllib2 # load settings file from buildsettings import buildSettings @@ -23,6 +23,7 @@ # load option local settings file try: from localbuildsettings import buildSettings as localBuildSettings + buildSettings.update(localBuildSettings) except ImportError: pass @@ -33,17 +34,15 @@ except ImportError: defaultBuild = None - buildName = defaultBuild # build name from command line -if len(sys.argv) == 2: # argv[0] = program, argv[1] = buildname, len=2 +if len(sys.argv) == 2: # argv[0] = program, argv[1] = buildname, len=2 buildName = sys.argv[1] - -if buildName is None or not buildName in buildSettings: - print ("Usage: build.py buildname") - print (" available build names: %s" % ', '.join(buildSettings.keys())) +if buildName is None or buildName not in buildSettings: + print("Usage: build.py buildname") + print(" available build names: %s" % ', '.join(buildSettings.keys())) sys.exit(1) settings = buildSettings[buildName] @@ -51,17 +50,16 @@ # set up vars used for replacements utcTime = time.gmtime() -buildDate = time.strftime('%Y-%m-%d-%H%M%S',utcTime) +buildDate = time.strftime('%Y-%m-%d-%H%M%S', utcTime) # userscripts have specific specifications for version numbers - the above date format doesn't match -dateTimeVersion = time.strftime('%Y%m%d.',utcTime) + time.strftime('%H%M%S',utcTime).lstrip('0') +dateTimeVersion = time.strftime('%Y%m%d.', utcTime) + time.strftime('%H%M%S', utcTime).lstrip('0') # extract required values from the settings entry resourceUrlBase = settings.get('resourceUrlBase') distUrlBase = settings.get('distUrlBase') buildMobile = settings.get('buildMobile') -gradleOptions = settings.get('gradleOptions','') -gradleBuildFile = settings.get('gradleBuildFile', 'mobile/build.gradle'); - +gradleOptions = settings.get('gradleOptions', '') +gradleBuildFile = settings.get('gradleBuildFile', 'mobile/build.gradle') # plugin wrapper code snippets. handled as macros, to ensure that # 1. indentation caused by the "function wrapper()" doesn't apply to the plugin code body @@ -103,31 +101,34 @@ def readfile(fn): with io.open(fn, 'Ur', encoding='utf8') as f: return f.read() + def loaderString(var): fn = var.group(1) return readfile(fn).replace('\\', '\\\\').replace('\n', '\\n').replace('\'', '\\\'') + def loaderRaw(var): fn = var.group(1) return readfile(fn) + def loaderImage(var): fn = var.group(1) - return 'data:image/png;base64,{0}'.format(base64.encodestring(open(fn, 'rb').read()).decode('utf8').replace('\n', '')) + return 'data:image/png;base64,{0}'.format( + base64.encodestring(open(fn, 'rb').read()).decode('utf8').replace('\n', '')) + def loadCode(ignore): return '\n\n;\n\n'.join(map(readfile, sorted(glob.glob('code/*.js')))) def extractUserScriptMeta(var): - m = re.search ( r"//[ \t]*==UserScript==\n.*?//[ \t]*==/UserScript==\n", var, re.MULTILINE|re.DOTALL ) + m = re.search(r"//[ \t]*==UserScript==\n.*?//[ \t]*==/UserScript==\n", var, re.MULTILINE | re.DOTALL) return m.group(0) - -def doReplacements(script,updateUrl,downloadUrl,pluginName=None): - - script = re.sub('@@INJECTCODE@@',loadCode,script) +def doReplacements(script, updateUrl, downloadUrl, pluginName=None): + script = re.sub('@@INJECTCODE@@', loadCode, script) script = script.replace('@@PLUGINSTART@@', pluginWrapperStart) script = script.replace('@@PLUGINSTART-USE-STRICT@@', pluginWrapperStartUseStrict) @@ -152,16 +153,16 @@ def doReplacements(script,updateUrl,downloadUrl,pluginName=None): script = script.replace('@@DOWNLOADURL@@', downloadUrl) if (pluginName): - script = script.replace('@@PLUGINNAME@@', pluginName); + script = script.replace('@@PLUGINNAME@@', pluginName) return script -def saveScriptAndMeta(script,ourDir,filename,oldDir=None): +def saveScriptAndMeta(script, ourDir, filename, oldDir=None): # TODO: if oldDir is set, compare files. if only data/time-based version strings are different # copy from there instead of saving a new file - fn = os.path.join(outDir,filename) + fn = os.path.join(outDir, filename) with io.open(fn, 'w', encoding='utf8') as f: f.write(script) @@ -174,18 +175,16 @@ def saveScriptAndMeta(script,ourDir,filename,oldDir=None): outDir = os.path.join('build', buildName) - # create the build output # first, delete any existing build - but keep it in a temporary folder for now oldDir = None if os.path.exists(outDir): - oldDir = outDir+'~'; + oldDir = outDir + '~' if os.path.exists(oldDir): shutil.rmtree(oldDir) os.rename(outDir, oldDir) - # copy the 'dist' folder, if it exists if os.path.exists('dist'): # this creates the target directory (and any missing parent dirs) @@ -195,32 +194,29 @@ def saveScriptAndMeta(script,ourDir,filename,oldDir=None): # no 'dist' folder - so create an empty target folder os.makedirs(outDir) - # run any preBuild commands -for cmd in settings.get('preBuild',[]): - os.system ( cmd ) - +for cmd in settings.get('preBuild', []): + os.system(cmd) # load main.js, parse, and create main total-conversion-build.user.js main = readfile('main.js') downloadUrl = distUrlBase and distUrlBase + '/total-conversion-build.user.js' or 'none' updateUrl = distUrlBase and distUrlBase + '/total-conversion-build.meta.js' or 'none' -main = doReplacements(main,downloadUrl=downloadUrl,updateUrl=updateUrl) +main = doReplacements(main, downloadUrl=downloadUrl, updateUrl=updateUrl) saveScriptAndMeta(main, outDir, 'total-conversion-build.user.js', oldDir) with io.open(os.path.join(outDir, '.build-timestamp'), 'w') as f: f.write(u"" + time.strftime('%Y-%m-%d %H:%M:%S UTC', utcTime)) - # for each plugin, load, parse, and save output -os.mkdir(os.path.join(outDir,'plugins')) +os.mkdir(os.path.join(outDir, 'plugins')) for fn in glob.glob("plugins/*.user.js"): script = readfile(fn) - downloadUrl = distUrlBase and distUrlBase + '/' + fn.replace("\\","/") or 'none' + downloadUrl = distUrlBase and distUrlBase + '/' + fn.replace("\\", "/") or 'none' updateUrl = distUrlBase and downloadUrl.replace('.user.js', '.meta.js') or 'none' pluginName = os.path.splitext(os.path.splitext(os.path.basename(fn))[0])[0] script = doReplacements(script, downloadUrl=downloadUrl, updateUrl=updateUrl, pluginName=pluginName) @@ -229,13 +225,13 @@ def saveScriptAndMeta(script,ourDir,filename,oldDir=None): # if we're building mobile too if buildMobile: - if buildMobile not in ['debug','release','copyonly']: + if buildMobile not in ['debug', 'release', 'copyonly']: raise Exception("Error: buildMobile must be 'debug' or 'release' or 'copyonly'") # compile the user location script fn = "user-location.user.js" script = readfile("mobile/plugins/" + fn) - downloadUrl = distUrlBase and distUrlBase + '/' + fn.replace("\\","/") or 'none' + downloadUrl = distUrlBase and distUrlBase + '/' + fn.replace("\\", "/") or 'none' updateUrl = distUrlBase and downloadUrl.replace('.user.js', '.meta.js') or 'none' script = doReplacements(script, downloadUrl=downloadUrl, updateUrl=updateUrl, pluginName='user-location') @@ -246,20 +242,19 @@ def saveScriptAndMeta(script,ourDir,filename,oldDir=None): os.makedirs("mobile/assets") except: pass - shutil.copy(os.path.join(outDir,"total-conversion-build.user.js"), "mobile/assets/total-conversion-build.user.js") + shutil.copy(os.path.join(outDir, "total-conversion-build.user.js"), "mobile/assets/total-conversion-build.user.js") # copy the user location script into the mobile folder. - shutil.copy(os.path.join(outDir,"user-location.user.js"), "mobile/assets/user-location.user.js") + shutil.copy(os.path.join(outDir, "user-location.user.js"), "mobile/assets/user-location.user.js") # also copy plugins try: shutil.rmtree("mobile/assets/plugins") except: pass - shutil.copytree(os.path.join(outDir,"plugins"), "mobile/assets/plugins", - # do not include desktop-only plugins to mobile assets - ignore=shutil.ignore_patterns('*.meta.js', - 'force-https*', 'speech-search*', 'basemap-cloudmade*', - 'scroll-wheel-zoom-disable*')) - + shutil.copytree(os.path.join(outDir, "plugins"), "mobile/assets/plugins", + # do not include desktop-only plugins to mobile assets + ignore=shutil.ignore_patterns('*.meta.js', + 'force-https*', 'speech-search*', 'basemap-cloudmade*', + 'scroll-wheel-zoom-disable*')) if buildMobile != 'copyonly': # now launch 'ant' to build the mobile project @@ -267,18 +262,17 @@ def saveScriptAndMeta(script,ourDir,filename,oldDir=None): retcode = os.system("mobile/gradlew %s -b %s %s" % (gradleOptions, gradleBuildFile, buildAction)) if retcode != 0: - print ("Error: mobile app failed to build. gradlew returned %d" % retcode) - exit(1) # ant may return 256, but python seems to allow only values <256 + print("Error: mobile app failed to build. gradlew returned %d" % retcode) + exit(1) # ant may return 256, but python seems to allow only values <256 else: appId = "org.exarhteam.iitc_mobile" if buildMobile == "debug": appId += ".debug" - shutil.copy("mobile/app/build/outputs/apk/%s/%s-%s.apk" % (buildMobile, appId, buildMobile), os.path.join(outDir,"IITC_Mobile-%s.apk" % buildMobile) ) - + shutil.copy("mobile/app/build/outputs/apk/%s/%s-%s.apk" % (buildMobile, appId, buildMobile), + os.path.join(outDir, "IITC_Mobile-%s.apk" % buildMobile)) # run any postBuild commands -for cmd in settings.get('postBuild',[]): - os.system ( cmd ) - +for cmd in settings.get('postBuild', []): + os.system(cmd) # vim: ai si ts=4 sw=4 sts=4 et diff --git a/buildsettings.py b/buildsettings.py index d35c9e325..5d2314e72 100644 --- a/buildsettings.py +++ b/buildsettings.py @@ -39,16 +39,15 @@ # if you want to publish your own fork of the project, and host it on your own web site # create a localbuildsettings.py file containing something similar to this # note: Firefox+Greasemonkey require the distUrlBase to be "https" - they won't check for updates on regular "http" URLs - #'example': { + # 'example': { # 'resourceBaseUrl': 'http://www.example.com/iitc/dist', # 'distUrlBase': 'https://secure.example.com/iitc/dist', - #}, + # }, } - # defaultBuild - the name of the default build to use if none is specified on the build.py command line # (in here as an example - it only works in localbuildsettings.py) #defaultBuild = 'local' diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 000000000..3432e947b --- /dev/null +++ b/setup.cfg @@ -0,0 +1,5 @@ +; Configuration files for python based utilities such as the pep8 linter. + +[pep8] +; E501 line too long +ignore = E501 \ No newline at end of file