def main(): args = sys.argv[1:] if len(args) < 1: logging.warning('Usage: python <filename> <function> <parameters..>') elif args[0] == 'init': init() elif args[0] == 'add': data_to_copy_path = args[1] add(data_to_copy_path) elif args[0] == 'commit': message = args[1] commit(message) elif args[0] == 'status': status() elif args[0] == 'checkout': commit_id = args[1] checkout(commit_id) elif args[0] == 'graph': graph() elif args[0] == 'branch': branch_name = args[1] branch(branch_name) elif args[0] == 'merge': branch_name = args[1] merge()
def merge(branch: str, wit_path: str): """Merges two commits in to a new commit.""" if _can_checkout: if is_branch(wit_path, branch): branch_on_head = _get_references_data(wit_path)["HEAD"] branch_id = _get_references_data(wit_path)[branch] if _get_head(wit_path) == branch_on_head: commit_id = commit( f'A merge between "{branch_on_head}" and "{branch}"', wit_path, branch_id) else: common_parent = _get_common_parent(wit_path, _get_head(wit_path), branch_id) _move_files(wit_path, common_parent, branch_id) commit_id = commit( f'A merge between "{branch_on_head}" and "{branch}"', wit_path, branch_id) checkout(branch_on_head, wit_path) logging.info( f'"{branch}" was successfully merged with {branch_on_head}.') print(commit_id) else: logging.error( f'"{branch}" is not a branch, try "branch" function to create a new branch.' )
def test_checkout_default(tmpdir): """ checkout operation should succeed, and point to the latest revision""" remote = setup_hg_repo(tmpdir.path, 'remote') output = setup_hg_repo(tmpdir.path) set_hg_repo_path(output, remote) checkout.checkout(output, remote) eq_(checkout.revision(output), '176e98c1d359')
def test_checkout_revision(tmpdir): """ checkout operation should succeed and point to some revision""" remote = setup_hg_repo(tmpdir.path, 'remote') output = setup_hg_repo(tmpdir.path) set_hg_repo_path(output, remote) checkout.checkout(output, remote, head_rev='123df26ca0f5') eq_(checkout.revision(output), '123df26ca0f5')
def checkout_test(Products, Cost): """assert sanity of the checkout function above""" flag = True total_cost_no_offers = 0 # sanity check total cost to evaluate case where no offer is active print("-------------Sanity Checking-------------") # check if the cost is correctly calculated for each singular appearance of each product # that is if : checkout(['B'], {'B': 40}) then total cost should be equal to the only cost defined for item in set(Products): # only check the unique elements cost = checkout([item], {item: Cost[item]}) if cost != Cost[item]: print("Error: the cost of item {} is not correctly calculated!". format(item)) flag = False else: print("Checking cost for item {}:".format(item) + " " + str(cost)) # assess if no offer is active for key in Cost: occ = Products.count(key) total_cost_no_offers += occ * Cost[key] if total_cost_no_offers == checkout(Products, Cost): print("Calculations do not take into account any offers!") flag = False print("***********************************************") print("Total assesement of sanity checks: " + str(flag)) print("***********************************************") return flag
def call_merge(project_root, repo_directory): import merge import checkout foreign_repo = merge.ask_user_repo_name(project_root, repo_directory) merge_id = merge.ask_user_for_commit_id(foreign_repo, repo_directory) #TODO I think we have to make a commit here checkout.checkout(project_root, repo_directory, merge_id, foreign_repo ) commit.commit("merge commit", project_root, repo_directory)
def test_checkout_group(self): self.assertEqual(checkout('SSS'), 45) self.assertEqual(checkout('XYZSST'), 90) self.assertEqual(checkout('TTTTTT'), 90) self.assertEqual(checkout('TTTTTTT'), 90 + 20) self.assertEqual(checkout('TTTZZZZ'), 90 + 20) self.assertEqual(checkout('ZZZZZZZ'), 90 + 21) self.assertEqual(checkout('TTTTTTTT'), 90 + 20 * 2) self.assertEqual(checkout('XXXXXXXX'), 90 + 17 * 2) self.assertEqual(checkout('SSSSSSSS'), 90 + 20 * 2)
def test_checkout_V(self): self.assertEqual(checkout('V'), 50) self.assertEqual(checkout('VV'), 90) self.assertEqual(checkout('VVV'), 130) self.assertEqual(checkout('VVVVV'), 130 + 90) self.assertEqual(checkout('VVVVVV'), 130 + 130) self.assertEqual(checkout('VVVVVVV'), 130 + 130 + 50)
def test_checkout_A(self): self.assertEqual(checkout('A'), 50) self.assertEqual(checkout('AAA'), 130) self.assertEqual(checkout('AAAa'), -1) self.assertEqual(checkout('AAAAA'), 200) self.assertEqual(checkout('AAAAAAAA'), 200 + 130) self.assertEqual(checkout('AAAAAAAAA'), 200 + 130 + 50)
def commands() -> None: """Redirects all the commands that require a wit path.""" wit_path = _search_parent_dir(".wit") _set_logger(os.path.join(wit_path, '.wit')) if sys.argv[1] == 'add': add(sys.argv[2], wit_path) elif sys.argv[1] == 'commit': print(commit(sys.argv[2], wit_path)) elif sys.argv[1] == 'status': print(status(wit_path)) elif sys.argv[1] == 'checkout': checkout(sys.argv[2], wit_path) elif sys.argv[1] == 'graph': graph(wit_path) elif sys.argv[1] == 'branch': branch(sys.argv[2], wit_path) elif sys.argv[1] == 'merge': merge(sys.argv[2], wit_path) else: logging.error( MethodNotFoundError(f'unrecognized method: "{sys.argv[1]}".'))
def test_calculate_correct_total(self): cart = {} self.assertEqual(checkout.checkout(cart), 0) cart = {'CH1': 1, 'AP1': 1} self.assertEqual(checkout.checkout(cart), 9.11) cart = {'CH1': 1, 'AP1': 1, 'CF1': 1, 'MK1': 1} self.assertEqual(checkout.checkout(cart), 20.34) cart = {'AP1': 1, 'MK1': 1} self.assertEqual(checkout.checkout(cart), 10.75) cart = {'CF1': 2} self.assertEqual(checkout.checkout(cart), 11.23) cart = {'CH1': 1, 'AP1': 3, 'MK1': 1} self.assertEqual(checkout.checkout(cart), 16.61) cart = {'AP1': 3, 'CH1': 1} self.assertEqual(checkout.checkout(cart), 16.61)
def test_sum2(): assert checkout('AbC') == -1
def test_sum1(): assert checkout('ABC') == 100
def test_sum7(): assert checkout('AAABB') == 170
def test_sum6(): assert checkout(18) == -1
def test_checkout_H(self): self.assertEqual(checkout('H'), 10) self.assertEqual(checkout('HHHHH'), 45) self.assertEqual(checkout('HHHHHHHHHH'), 80) self.assertEqual(checkout('HHHHHHHHHHHHHHH'), 80 + 45) self.assertEqual(checkout('HHHHHHHHHHHHHHHH'), 80 + 45 + 10)
def run_script_line(s, is_config, cfg): global delegatefps # Execute the script line given in s. # Trim the newline off the end of the string, to begin with. while s[-1:] == "\r" or s[-1:] == "\n": s = s[:-1] w, sr = lexer.get_word(s, cfg) if w == None or w == "": return # no command on this line # Log every line executed by a non-config script. if not is_config: log.logscript(s) if w == "ifeq" or w == "ifneq": w1, sr = lexer.get_word(sr, cfg) w2, sr = lexer.get_word(sr, cfg) log.logmsg("testing string equality of `%s' and `%s'" % (w1, w2)) if (w1 == w2) != (w == "ifeq"): return # condition not taken w, sr = lexer.get_word(sr, cfg) # now read the main command if w == "ifexist" or w == "ifnexist": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) w1, sr = lexer.get_word(sr, cfg) log.logmsg("testing existence of `%s'" % w1) if (os.path.exists(os.path.join(cfg.workpath, w1)) != 0) != (w == "ifexist"): return # condition not taken w, sr = lexer.get_word(sr, cfg) # now read the main command if w == "set": # Set a variable. var, val = lexer.get_word(sr, cfg) val = lexer.lex_all(lexer.trim(val), cfg) if not is_config: log.logmsg("Setting variable `%s' to value `%s'" % (var, val)) lexer.set_multicharvar(var, val) elif w == "read": # Set a variable by reading from a file. var, sr = lexer.get_word(sr, cfg) filename, sr = lexer.get_word(sr, cfg) filename = os.path.join(cfg.workpath, filename) if not is_config: log.logmsg("Reading file `%s'" % (filename)) with open(filename, "r") as f: val = f.read() val = val.rstrip("\r\n") if not is_config: log.logmsg("Setting variable `%s' to value `%s'" % (var, val)) lexer.set_multicharvar(var, val) elif w == "in" or w == "in-dest": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) if delegatefps != None and w != "in": raise misc.builderr("`in-dest' command invalid during delegation" % w) dir, sr = lexer.get_word(sr, cfg) do, sr = lexer.get_word(sr, cfg) if do != "do": raise misc.builderr("expected `do' after `%s'" % w) cmd = lexer.lex_all(lexer.trim(sr), cfg) if delegatefps != None: log.logmsg("Running command on delegate server: " + cmd) # Instead of running the command locally, send it to # the delegate host, and receive in return some output # and an exit code. delegatefps[0].write("C" + struct.pack(">L", len(dir)) + dir + struct.pack(">L", len(cmd)) + cmd) delegatefps[0].flush() # Retrieve the build command's output, line by line. output = "" while 1: outlen = delegatefps[1].read(4) if len(outlen) < 4: raise misc.builderr("unexpected EOF from delegate server") outlen = struct.unpack(">L", outlen)[0] if outlen == 0: break outchunk = delegatefps[1].read(outlen) if len(outchunk) < outlen: raise misc.builderr("unexpected EOF from delegate server") output = output + outchunk while 1: newline = string.find(output, "\n") if newline < 0: break line = output[:newline] output = output[newline + 1:] while line[-1:] == "\r" or line[-1:] == "\n": line = line[:-1] log.logoutput(line) # Log the final partial line, if any. if len(output) > 0: while output[-1:] == "\r" or output[-1:] == "\n": output = output[:-1] log.logoutput(output) exitcode = delegatefps[1].read(4) if len(exitcode) < 4: raise misc.builderr("unexpected EOF from delegate server") exitcode = struct.unpack(">l", exitcode)[0] if exitcode > 0: raise misc.builderr("build command terminated with status %d" % exitcode) else: if w == "in-dest": dir = os.path.join(cfg.outpath, dir) else: dir = os.path.join(cfg.workpath, dir) log.logmsg("Running command in directory `%s': %s" % (dir, cmd)) cmd = misc.shellquote(["cd", dir]) + " && " + cmd f = os.popen(cmd + " 2>&1", "r") while 1: line = f.readline() if line == "": break while line[-1:] == "\r" or line[-1:] == "\n": line = line[:-1] log.logoutput(line) ret = f.close() if ret > 0: raise misc.builderr("build command terminated with status %d" % ret) elif w == "deliver": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) srcpath, sr = lexer.get_word(sr, cfg) sr = lexer.trim(sr) nfiles = 0 for srcfile in glob.glob(os.path.join(cfg.workpath, srcpath)): save = lexer.save_vars() lexer.set_onecharvar("@", os.path.basename(srcfile)) dstfile, sx = lexer.get_word(sr, cfg) lexer.restore_vars(save) dstfile = os.path.join(cfg.outpath, dstfile) log.logmsg("Delivering `%s' to `%s'" % (srcfile, dstfile)) dstdir = os.path.dirname(dstfile) if not os.path.exists(dstdir): os.makedirs(dstdir) shutil.copyfile(srcfile, dstfile) nfiles = nfiles + 1 if nfiles == 0: raise misc.builderr("deliver statement did not match any files") elif w == "checkout": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) module, sr = lexer.get_word(sr, cfg) destdir, sr = lexer.get_word(sr, cfg) if module == None or destdir == None: raise misc.builderr("`checkout' command expects two parameters") destdir = os.path.join(cfg.workpath, destdir) checkout.checkout(cfg, module, destdir, 0) elif w == "module": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) newmodule, sr = lexer.get_word(sr, cfg) if newmodule == None: raise misc.builderr("`module' command expects a parameter") srcdir = os.path.join(cfg.workpath, cfg.mainmodule) destdir = os.path.join(cfg.workpath, newmodule) if srcdir == destdir: log.logmsg("main module already has correct filename") else: log.logmsg("renaming main module directory `%s' to `%s'" % (srcdir, destdir)) os.rename(srcdir, destdir) cfg.mainmodule = newmodule cfg.seen_module = 1 elif w == "delegate": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) hosttype, sr = lexer.get_word(sr, cfg) if hosttype == None: raise misc.builderr("expected a host type after `delegate'") if delegatefps != None: raise misc.builderr("a delegation session is already open") # Read the config file to find out what actual host to # connect to for the given host type. save = lexer.save_vars() process_script(cfg.cfgfile, 1, cfg) host = lexer.get_multicharvar("host_" + hosttype) sshid = lexer.get_multicharvar("id_" + hosttype) usercmd = lexer.get_multicharvar("cmd_" + hosttype) lexer.restore_vars(save) if host == "": raise misc.builderr( "configuration does not specify a host for delegate type `%s'" % hosttype) # Open a connection to the delegate host. log.logmsg("Starting delegation to host type `%s'" % hosttype) if usercmd != None: delcmd = usercmd else: if hosttype == "-": # Special case: a host name of "-" causes a # self-delegation, i.e. we invoke the delegate # server directly rather than bothering with ssh. for pdir in sys.path: delcmd = pdir + "/" + name.server if os.path.exists(delcmd): break delcmd = None if delcmd == None: raise misc.builderr("unable to find delegate server") delcmd = [delcmd] else: delcmd = ["ssh"] # If the user has specified an SSH identity key, use it. if sshid != None: delcmd = ["SSH_AUTH_SOCK="] + delcmd + ["-i", sshid] delcmd.append(host) delcmd.append(name.server) delcmd = misc.shellquote(delcmd) log.logmsg(" Running delegation command: " + delcmd) delegatefps = popen2(delcmd) # Wait for the announcement from the far end which says the # delegate server is running. while 1: s = delegatefps[1].readline() if s == "": raise misc.builderr("unexpected EOF from delegate server") while s[-1:] == "\r" or s[-1:] == "\n": s = s[:-1] if s == name.server_banner: log.logmsg(" Successfully started delegate server") break # Send a tarball of our build work directory. tarpipe = os.popen( misc.shellquote(["tar", "-C", cfg.workpath, "-czf", "-", "."]), "r") data = tarpipe.read() tarpipe.close() delegatefps[0].write("T" + struct.pack(">L", len(data)) + data) delegatefps[0].flush() elif w == "return": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) if delegatefps == None: raise misc.builderr("no delegation session open") # Copy file(s) back from the delegate host. We send our # command character "R", then a glob pattern; we then # repeatedly read a filename and file contents until we # receive zero filename length. pattern, sr = lexer.get_word(sr, cfg) if pattern == None: raise misc.builderr("expected a file name after `return'") delegatefps[0].write("R" + struct.pack(">L", len(pattern)) + pattern) delegatefps[0].flush() nfiles = 0 while 1: fnamelen = delegatefps[1].read(4) if len(fnamelen) < 4: raise misc.builderr("unexpected EOF from delegate server") fnamelen = struct.unpack(">L", fnamelen)[0] if fnamelen == 0: break fname = delegatefps[1].read(fnamelen) if len(fname) < fnamelen: raise misc.builderr("unexpected EOF from delegate server") datalen = delegatefps[1].read(4) if len(datalen) < 4: raise misc.builderr("unexpected EOF from delegate server") datalen = struct.unpack(">L", datalen)[0] data = delegatefps[1].read(datalen) if len(data) < datalen: raise misc.builderr("unexpected EOF from delegate server") log.logmsg("Returned file `%s' from delegate server" % fname) #' # Vet the filename for obvious gotchas. if string.find("/" + fname + "/", "/../") >= 0 or fname[:1] == "/": raise misc.builderr( "returned file `%s' failed security check" % fname) #' dstfile = os.path.join(cfg.workpath, fname) dstdir = os.path.dirname(dstfile) if not os.path.exists(dstdir): os.makedirs(dstdir) outfp = open(dstfile, "wb") outfp.write(data) outfp.close() nfiles = nfiles + 1 if nfiles == 0: raise misc.builderr("return statement did not match any files") elif w == "enddelegate": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) if delegatefps == None: raise misc.builderr("no delegation session open") # Close the delegate session delegatefps[0].write("Q") delegatefps[0].close() delegatefps[1].close() delegatefps = None log.logmsg("Closed delegate session") else: raise misc.builderr("unrecognised statement keyword `%s'" % w)
from PyQt5.QtWidgets import * import sys from welcome import welcome from checkout import checkout app = QApplication(sys.argv) screen = checkout() checkout.populate_packages(screen,dict()) screen.show() sys.exit(app.exec_())
prev_hash = b'' # Initialise necessary arguements if action not in ["init", "verify"]: if action == "add": arguements["case_id"] = args.c arguements["item_id"] = args.i if arguements["case_id"] and arguements["item_id"]: insert(arguements["case_id"], arguements["item_id"], file_path) else: Arguement_Error() elif action == "checkout" or action == "checkin": arguements["item_id"] = args.i if action == "checkout": checkout(arguements["item_id"], file_path) else: checkin(arguements["item_id"], file_path) elif action == "log": arguements["reverse"] = args.reverse arguements["number"] = args.n arguements["case_id"] = args.c arguements["item_id"] = args.i log(arguements["reverse"], arguements["number"], arguements["case_id"], arguements["item_id"], file_path) #log call else: arguements["item_id"] = args.i arguements["reason"] = args.why arguements["owner"] = args.o if (arguements["reason"] == "RELEASED"):
def test_checkout_E(self): self.assertEqual(checkout('EE'), 40 * 2) self.assertEqual(checkout('EEB'), 40 * 2) self.assertEqual(checkout('EEBB'), 40 * 2 + 30)
#cfg.version_list=["cabecd188bb18003ada7c9470d005ac007d1be2c","597e4d662bb9f56cc9f1005d00210293072b5066"] #List of versions cfg.version_list=["7518","7525"] #Results cfg.log_file='/home/rob/Desktop/mesaTest.log' #Somewhere to build MESA cfg.temp_fold='/media/data/mesa/temp/' cfg.mesasdk_root='/media/data/mesa/sdk/mesasdk-20141212' cfg.omp_num_threads='8' #Ignore for now cfg.vcs_mode='svn' cfg.vcs_git_base_folder='/media/data/mesa/mesa/dev/' for cfg.version in cfg.version_list: print("Running "+cfg.version) cfg.setDefaults() cfg.setPaths() log=l.logger(cfg) check=c.checkout(cfg) gb=b.build(cfg) tt=t.test(cfg) log.writeLog(cfg) cfg.cleanup() print("Done "+cfg.version)
def test_checkout_F(self): self.assertEqual(checkout('FF'), 10 * 2) self.assertEqual(checkout('FFF'), 10 * 2)
def test_checkout_with_double_special_offer(self): self.assertEqual(checkout('AAAAAAAAAAAAA'), 530)
import sys from confidence import confidence from baking import baking from produce import produce from checkout import checkout name = "Laura" conf = confidence(name) # #baking # bakedGoods = baking(name, conf) # bakedGoods.bakingAisle() #produce aisle # produceSection = produce(name, conf) # produceSection.produceAisle() #checkout checkOut = checkout(name, conf, "something else", "special") checkOut.check_out()
def test_sum4(): assert checkout('DDA') == 80
def test_sum5(): assert checkout('AAA') == 120
def test_checkout_function(scanned_codes, prices, expected): total = checkout.checkout(scanned_codes, prices) assert total == expected
def test_sum6(): assert checkout('BB') == 50
produceSection = produce(name, conf) produceSection.produceAisle() aisles_visited.add("B. Produce") elif aisles == "C": # canned goods print( f"\n{name} stands at the entrance to the canned goods aisle. There's nothing on the grocery list to get from this aisle, but they can enter if they wish." ) enter = input( f"\nShould {name} enter the aisle? \nA: Yes \nB: No \nAnswer: ") enter = enter.upper() if enter == "A": cannedGoods = canned(name, conf) cannedGoods.cannedAisle() aisles_visited.add("C. Canned Goods") special_ingredient = "special" elif enter == "B": print( f"\n{name} turns away, the shimmering, well-organized aisle of cans upon cans now behind them." ) special_ingredient = "none" aisles_visited.add("C. Canned Goods") elif aisles == "D": frozenSection = frozen(name, conf, specialItem) frozenSection.frozen_aisle() aisles_visited.add("D. Frozen") input(f"{name} has gathered all items needed and can now head to checkout.") checkOut = checkout(name, conf, specialItem, special_ingredient) checkOut.check_out()
def call_checkout(project_root, repo_directory): import checkout checkout.checkout(project_root, repo_directory)
def test_sum3(): assert checkout('BCD') == 65
def run_script_line(s, is_config, cfg): global delegatefps # Execute the script line given in s. # Trim the newline off the end of the string, to begin with. while s[-1:] == "\r" or s[-1:] == "\n": s = s[:-1] w, sr = lexer.get_word(s, cfg) if w == None or w == "": return # no command on this line # Log every line executed by a non-config script. if not is_config: log.logscript(s) if w == "ifeq" or w == "ifneq": w1, sr = lexer.get_word(sr, cfg) w2, sr = lexer.get_word(sr, cfg) log.logmsg("testing string equality of `%s' and `%s'" % (w1, w2)) if (w1 == w2) != (w == "ifeq"): return # condition not taken w, sr = lexer.get_word(sr, cfg) # now read the main command if w == "ifexist" or w == "ifnexist": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) w1, sr = lexer.get_word(sr, cfg) log.logmsg("testing existence of `%s'" % w1) if (os.path.exists(os.path.join(cfg.workpath,w1))!=0) != (w=="ifexist"): return # condition not taken w, sr = lexer.get_word(sr, cfg) # now read the main command if w == "set": # Set a variable. var, val = lexer.get_word(sr, cfg) val = lexer.lex_all(lexer.trim(val), cfg) if not is_config: log.logmsg("Setting variable `%s' to value `%s'" % (var,val)) lexer.set_multicharvar(var, val) elif w == "read": # Set a variable by reading from a file. var, sr = lexer.get_word(sr, cfg) filename, sr = lexer.get_word(sr, cfg) filename = os.path.join(cfg.workpath, filename) if not is_config: log.logmsg("Reading file `%s'" % (filename)) with open(filename, "r") as f: val = f.read() val = val.rstrip("\r\n") if not is_config: log.logmsg("Setting variable `%s' to value `%s'" % (var,val)) lexer.set_multicharvar(var, val) elif w == "in" or w == "in-dest": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) if delegatefps != None and w != "in": raise misc.builderr("`in-dest' command invalid during delegation" % w) dir, sr = lexer.get_word(sr, cfg) do, sr = lexer.get_word(sr, cfg) if do != "do": raise misc.builderr("expected `do' after `%s'" % w) cmd = lexer.lex_all(lexer.trim(sr), cfg) if delegatefps != None: log.logmsg("Running command on delegate server: " + cmd) # Instead of running the command locally, send it to # the delegate host, and receive in return some output # and an exit code. delegatefps[0].write("C" + struct.pack(">L", len(dir)) + dir + struct.pack(">L", len(cmd)) + cmd) delegatefps[0].flush() # Retrieve the build command's output, line by line. output = "" while 1: outlen = delegatefps[1].read(4) if len(outlen) < 4: raise misc.builderr("unexpected EOF from delegate server") outlen = struct.unpack(">L", outlen)[0] if outlen == 0: break outchunk = delegatefps[1].read(outlen) if len(outchunk) < outlen: raise misc.builderr("unexpected EOF from delegate server") output = output + outchunk while 1: newline = string.find(output, "\n") if newline < 0: break line = output[:newline] output = output[newline+1:] while line[-1:] == "\r" or line[-1:] == "\n": line = line[:-1] log.logoutput(line) # Log the final partial line, if any. if len(output) > 0: while output[-1:] == "\r" or output[-1:] == "\n": output = output[:-1] log.logoutput(output) exitcode = delegatefps[1].read(4) if len(exitcode) < 4: raise misc.builderr("unexpected EOF from delegate server") exitcode = struct.unpack(">l", exitcode)[0] if exitcode > 0: raise misc.builderr("build command terminated with status %d" % exitcode) else: if w == "in-dest": dir = os.path.join(cfg.outpath, dir) else: dir = os.path.join(cfg.workpath, dir) log.logmsg("Running command in directory `%s': %s" % (dir, cmd)) cmd = misc.shellquote(["cd", dir]) + " && " + cmd f = os.popen(cmd + " 2>&1", "r") while 1: line = f.readline() if line == "": break while line[-1:] == "\r" or line[-1:] == "\n": line = line[:-1] log.logoutput(line) ret = f.close() if ret > 0: raise misc.builderr("build command terminated with status %d" % ret) elif w == "deliver": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) srcpath, sr = lexer.get_word(sr, cfg) sr = lexer.trim(sr) nfiles = 0 for srcfile in glob.glob(os.path.join(cfg.workpath, srcpath)): save = lexer.save_vars() lexer.set_onecharvar("@", os.path.basename(srcfile)) dstfile, sx = lexer.get_word(sr, cfg) lexer.restore_vars(save) dstfile = os.path.join(cfg.outpath, dstfile) log.logmsg("Delivering `%s' to `%s'" % (srcfile, dstfile)) dstdir = os.path.dirname(dstfile) if not os.path.exists(dstdir): os.makedirs(dstdir) shutil.copyfile(srcfile, dstfile) nfiles = nfiles + 1 if nfiles == 0: raise misc.builderr("deliver statement did not match any files") elif w == "checkout": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) module, sr = lexer.get_word(sr, cfg) destdir, sr = lexer.get_word(sr, cfg) if module == None or destdir == None: raise misc.builderr("`checkout' command expects two parameters") destdir = os.path.join(cfg.workpath, destdir) checkout.checkout(cfg, module, destdir, 0) elif w == "module": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) newmodule, sr = lexer.get_word(sr, cfg) if newmodule == None: raise misc.builderr("`module' command expects a parameter") srcdir = os.path.join(cfg.workpath, cfg.mainmodule) destdir = os.path.join(cfg.workpath, newmodule) if srcdir == destdir: log.logmsg("main module already has correct filename") else: log.logmsg("renaming main module directory `%s' to `%s'" % (srcdir, destdir)) os.rename(srcdir, destdir) cfg.mainmodule = newmodule cfg.seen_module = 1 elif w == "delegate": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) hosttype, sr = lexer.get_word(sr, cfg) if hosttype == None: raise misc.builderr("expected a host type after `delegate'") if delegatefps != None: raise misc.builderr("a delegation session is already open") # Read the config file to find out what actual host to # connect to for the given host type. save = lexer.save_vars() process_script(cfg.cfgfile, 1, cfg) host = lexer.get_multicharvar("host_" + hosttype) sshid = lexer.get_multicharvar("id_" + hosttype) usercmd = lexer.get_multicharvar("cmd_" + hosttype) lexer.restore_vars(save) if host == "": raise misc.builderr("configuration does not specify a host for delegate type `%s'" % hosttype) # Open a connection to the delegate host. log.logmsg("Starting delegation to host type `%s'" % hosttype) if usercmd != None: delcmd = usercmd else: if hosttype == "-": # Special case: a host name of "-" causes a # self-delegation, i.e. we invoke the delegate # server directly rather than bothering with ssh. for pdir in sys.path: delcmd = pdir + "/" + name.server if os.path.exists(delcmd): break delcmd = None if delcmd == None: raise misc.builderr("unable to find delegate server") delcmd = [delcmd] else: delcmd = ["ssh"] # If the user has specified an SSH identity key, use it. if sshid != None: delcmd = ["SSH_AUTH_SOCK="] + delcmd + ["-i", sshid] delcmd.append(host) delcmd.append(name.server) delcmd = misc.shellquote(delcmd) log.logmsg(" Running delegation command: " + delcmd) delegatefps = popen2(delcmd) # Wait for the announcement from the far end which says the # delegate server is running. while 1: s = delegatefps[1].readline() if s == "": raise misc.builderr("unexpected EOF from delegate server") while s[-1:] == "\r" or s[-1:] == "\n": s = s[:-1] if s == name.server_banner: log.logmsg(" Successfully started delegate server") break # Send a tarball of our build work directory. tarpipe = os.popen(misc.shellquote(["tar", "-C", cfg.workpath, "-czf", "-", "."]), "r") data = tarpipe.read() tarpipe.close() delegatefps[0].write("T" + struct.pack(">L", len(data)) + data) delegatefps[0].flush() elif w == "return": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) if delegatefps == None: raise misc.builderr("no delegation session open") # Copy file(s) back from the delegate host. We send our # command character "R", then a glob pattern; we then # repeatedly read a filename and file contents until we # receive zero filename length. pattern, sr = lexer.get_word(sr, cfg) if pattern == None: raise misc.builderr("expected a file name after `return'") delegatefps[0].write("R" + struct.pack(">L", len(pattern)) + pattern) delegatefps[0].flush() nfiles = 0 while 1: fnamelen = delegatefps[1].read(4) if len(fnamelen) < 4: raise misc.builderr("unexpected EOF from delegate server") fnamelen = struct.unpack(">L", fnamelen)[0] if fnamelen == 0: break fname = delegatefps[1].read(fnamelen) if len(fname) < fnamelen: raise misc.builderr("unexpected EOF from delegate server") datalen = delegatefps[1].read(4) if len(datalen) < 4: raise misc.builderr("unexpected EOF from delegate server") datalen = struct.unpack(">L", datalen)[0] data = delegatefps[1].read(datalen) if len(data) < datalen: raise misc.builderr("unexpected EOF from delegate server") log.logmsg("Returned file `%s' from delegate server" % fname) #' # Vet the filename for obvious gotchas. if string.find("/"+fname+"/", "/../") >= 0 or fname[:1] == "/": raise misc.builderr("returned file `%s' failed security check" % fname) #' dstfile = os.path.join(cfg.workpath, fname) dstdir = os.path.dirname(dstfile) if not os.path.exists(dstdir): os.makedirs(dstdir) outfp = open(dstfile, "wb") outfp.write(data) outfp.close() nfiles = nfiles + 1 if nfiles == 0: raise misc.builderr("return statement did not match any files") elif w == "enddelegate": if is_config: raise misc.builderr("`%s' command invalid in config file" % w) if not cfg.seen_module: raise misc.builderr("`%s' command seen before `module' command" % w) if delegatefps == None: raise misc.builderr("no delegation session open") # Close the delegate session delegatefps[0].write("Q") delegatefps[0].close() delegatefps[1].close() delegatefps = None log.logmsg("Closed delegate session") else: raise misc.builderr("unrecognised statement keyword `%s'" % w)