def build(solution, target): """build target of solution""" global configs msbuildexe = getMsBuildExe(solution) if not msbuildexe: myexit("MSBuild.exe not found.") args = [ msbuildexe, solution, # "/t:zzzDistResource", # "/p:Configuration=zzzDist", # "/p:Platform={}".format(target["platform"]) ] if "platform" in target: args.append("/p:platform={}".format(target["platform"])) if "setoutdirforbuild" in target and target["setoutdirforbuild"]: outDirBuild = os.path.abspath(target["outdir"]) outDirBuild = os.path.join( outDirBuild, '') # will add the trailing slash if it's not already there. args.append('/p:outdir={}'.format(outDirBuild)) if "targetproject" in configs: args.append("/t:{}".format(configs["targetproject"])) if "configuration" in configs: args.append("/p:Configuration={}".format(configs["configuration"])) print(args) subprocess.check_call(args)
def checkTarget(target): global configs outdir = target['outdir'] print("=== Start Testing {} ===".format(outdir)) if "ShouldNotBeFiles" in configs: checkShouldnotExistFile(outdir, configs["ShouldNotBeFiles"]) if "ShouldBeFiles" in configs: checkShouldBeFiles(outdir, configs["ShouldBeFiles"]) if "ShouldBeOneOfThem" in configs: checkShouldOneOfFiles(outdir, configs["ShouldBeOneOfThem"]) shouldBeFull = getAsFullpath(configs["ShouldBeFiles"], outdir) if (('TotalFileCount' not in configs) or configs['TotalFileCount'] == "exact"): showDiffAndExit(outdir, shouldBeFull, configs['TotalFileCount'], True) elif (isinstance(configs['TotalFileCount'], int)): if (configs['TotalFileCount'] != getFileCount(outdir)): showDiffAndExit(outdir, shouldBeFull, configs['TotalFileCount'], False) else: myexit("[TotalFileCount] must be int or 'exact'") print("Total file count = {}".format(getFileCount(outdir)))
def upload(self): configs = self.configs archiveexefull = self.archiveFull print("==== Uploading to {}... ====".format(configs["remotedir"])) daver.dupload(configs["remotedir"], archiveexefull) print("Uploaded to {}".format(configs["remotedir"])) print("==== Compute sha1 and compare... ====") localSha1 = getSha1(archiveexefull) remoteSha1Url = configs["remotesha1"].format(self.archiveexe) for loop in range(100): try: remoteSha1 = urllib.request.urlopen( remoteSha1Url).read().decode("utf-8") break except: print( "failed {} times to check remote Sha1. Will try again after waiting 5 seconds." .format(loop + 1)) time.sleep(5) # wait 5 seconds if localSha1.lower() != remoteSha1.lower(): myexit("sha1 not equal ({} != {}".format(localSha1, remoteSha1)) print("sha1 check succeed ({})".format(localSha1))
def checkShouldBeFiles(distDir, shouldbe): for f in shouldbe: fullpath = os.path.join(distDir, f) if (not (isfile(fullpath))): myexit(fullpath + " not exists.") return False return True
def checkArchivingDir(archivingfull, outdirsfull): ''' archivingfull must constans dirs only ''' # traverse root directory, and list directories as dirs and files as files dirs = os.listdir(archivingfull) dirsfull = [os.path.join(archivingfull, dir) for dir in dirs] # compare efficiently if Counter(dirsfull) != Counter(outdirsfull): myexit('archivingdir is not equal to outdirs')
def checkShouldnotExistFile(distDir, shouldnot): """ Return false if a file that should not be distributed exists. """ for f in shouldnot: fullpath = os.path.join(distDir, f) if isfile(fullpath) or isdir(fullpath): myexit(fullpath + " exists.") return False return True
def checkAlreadyUploaded(self, verstring): """ check remote archive exist and quit if true. """ configs = self.configs url = configs['remotedir'] + self.getArchiveName(verstring) print("checking {} ...".format(url)) if IsRemoteArchiveExists(url): myexit( "Archive already exists in remote site {0}. quitting.".format( url))
def getDevenvExeOrCom(solution, ext='.com'): vsvar = getVsVerFromSln(solution) if not vsvar: myexit('Could not find VS version from solution') pf = getDevenvExeOrCom2(getenv("ProgramFiles"), vsvar, ext) if pf and isfile(pf): return pf pf = getDevenvExeOrCom2(getenv("ProgramFiles(x86)"), vsvar, ext) if pf and isfile(pf): return pf return None
def getMsBuildExe(solution): vsvar = getVsVerFromSln(solution) if not vsvar: myexit('Could not find VS version from solution') pf = getMsBuildExe2(getenv("ProgramFiles"), vsvar) if pf and isfile(pf): return pf pf = getMsBuildExe2(getenv("ProgramFiles(x86)"), vsvar) if pf and isfile(pf): return pf return None
def getVersionString(target): """get version string from history.txt""" global configs outdir = target['outdir'] fileName = os.path.join(outdir, configs["obtainverfrom"]) with open(fileName, "r", encoding="utf-8") as f: lines = f.readlines() for line in lines: regstr = configs["obtainverregex"] m = re.search(regstr, line) if (m): return m.group(0) myexit("Version not found.")
def checkShouldOneOfFiles(self, distDir, shouldone): if shouldone: oneofthem = False for f in shouldone: fullpath = distDir + f if (os.path.isfile(fullpath)): if (oneofthem): myexit(fullpath + " One of them files duplicating.") return False oneofthem = True if (not oneofthem): myexit("None of oneofthem files exists.") return False return True
def getVersionString(self, outdir): """get version string from history.txt""" if self.__version: return self.__version configs = self.configs fileName = os.path.join(outdir, configs["obtainverfrom"]) with open(fileName, "r", encoding="utf-8") as f: lines = f.readlines() line = lines[0] regstr = configs["obtainverregex"] m = re.search(regstr, line) self.__version = m.group(0) return self.__version myexit("Version not found.")
def createArchive(self, path7z, targetdir, verstring): # configs = self.configs archiveexefull = self.getArchiveFull(verstring) if os.path.isfile(archiveexefull): myexit( '{} already exists, remove it first.'.format(archiveexefull)) args = [ path7z, "a", "-sfx7z.sfx", archiveexefull, targetdir, ] args.append("-mx9") print(args) subprocess.check_call(args)
def main(): if sys.version_info[0] < 3: myexit("Please use python3" + sys.version) print('{} {} ({})'.format(APPNAME, VERSION, APPDISC)) parser = ArgumentParser(prog="distSolution", description="Build VS Project") parser.add_argument("-C", nargs='?', action="store", help="Set current directory.") parser.add_argument("--skip-build", action="store_true", help="skip build process") parser.add_argument("--skip-check", action="store_true", help="skip check process") parser.add_argument("--skip-archive", action="store_true", help="skip archive process") parser.add_argument("--skip-upload", action="store_true", help="skip upload process") parser.add_argument("--skip-hashcheck", action="store_true", help="skip hashcheck process") parser.add_argument("--skip-bbs", action="store_true", help="skip bbs process") parser.add_argument('main') commandargs = parser.parse_args() if commandargs.C: os.chdir(commandargs.C) distFile = commandargs.main global configs print("Opening input {}".format(distFile)) with open(distFile, encoding="utf-8") as data_file: configs = json.load(data_file) solutionFile = os.path.join(os.path.dirname(distFile), configs["solution"]) verstring = "" # build first if not commandargs.skip_build: for target in configs['targets']: build(solutionFile, target) # check for target in configs['targets']: if not commandargs.skip_check: checkTarget(target) vstT = getVersionString(target) if (verstring and verstring != vstT): myexit("different is verstion between targets.") verstring = vstT #archive it archiveexe = "{}-{}{}".format(configs["name"], verstring, ".exe") archiveexefull = os.path.join(configs["archivedir"], archiveexe) if not commandargs.skip_archive: if isfile(archiveexefull): myexit( '{} already exists, remove it first.'.format(archiveexefull)) urlfull = configs['remotedir'] + archiveexe if not commandargs.skip_upload: if IsRemoteArchiveExists(urlfull): myexit('{} already exists'.format(urlfull)) if not commandargs.skip_archive: print("==== creating arhive {} ====".format(archiveexefull)) args7z = [ r"C:/LegacyPrograms/7-Zip/7z.exe", "a", "-sfx7z.sfx", archiveexefull, ] if 'archivingdir' in configs: # check the archivingdir only contains outdirs archivingfull = os.path.abspath(configs['archivingdir']) outdirsfull = [] for target in configs['targets']: outdirsfull.append(os.path.abspath(target['outdir'])) checkArchivingDir(archivingfull, outdirsfull) args7z.append(archivingfull) else: # no duplicate in args7z addedtarget = [] for t in configs['targets']: outdir = t['outdir'] if outdir not in addedtarget: outdirfull = os.path.abspath(outdir) args7z.append(outdirfull) addedtarget.append(outdir) args7z.append("-mx9") print(args7z) subprocess.check_call(args7z) # upload if not commandargs.skip_upload: print("==== Uploading to {}... ====".format(configs["remotedir"])) daver.dupload(configs["remotedir"], archiveexefull) print("Uploaded to {}".format(configs["remotedir"])) if not commandargs.skip_hashcheck: print("==== Compute sha1 and compare... ====") localSha1 = getSha1(archiveexefull) remoteSha1Url = configs["remotesha1"].format(archiveexe) for loop in range(100): try: remoteSha1 = urllib.request.urlopen( remoteSha1Url).read().decode("utf-8") break except: print( "failed {} times to check remote Sha1. Will try again after waiting 5 seconds." .format(loop + 1)) time.sleep(5) # wait 5 seconds if localSha1.lower() != remoteSha1.lower(): myexit("sha1 not equal ({} != {}".format(localSha1, remoteSha1)) print("sha1 check succeed ({})".format(localSha1)) ## update BBS if not commandargs.skip_bbs: print("==== Updating BBS... ====") historyFull = os.path.join(configs['targets'][0]['outdir'], configs['obtainverfrom']) versionReg = configs['obtainverregex'] print( updateBBS(configs['name'], verstring, configs["remotedir"] + archiveexe, getChangeLog(historyFull, versionReg)))