"""Utilities for tests only. These are not or should not be used normally -functions here are crafted as we don't want to use ``vcs`` to verify tests."""importosimportreimportsysfromsubprocessimportPopenclassVCSTestError(Exception):passdefrun_command(cmd,args):""" Runs command on the system with given ``args``. """command=' '.join((cmd,args))p=Popen(command,shell=True)status=os.waitpid(p.pid,0)[1]returnstatusdefeprint(msg):""" Prints given ``msg`` into sys.stderr as nose test runner hides all output from sys.stdout by default and if we want to pipe stream somewhere we don't need those verbose messages anyway. Appends line break. """sys.stderr.write(msg)sys.stderr.write('\n')classSCMFetcher(object):def__init__(self,alias,test_repo_path,remote_repo,clone_cmd):""" :param clone_cmd: command which would clone remote repository; pass only first bits - remote path and destination would be appended using ``remote_repo`` and ``test_repo_path`` """self.alias=aliasself.test_repo_path=test_repo_pathself.remote_repo=remote_repoself.clone_cmd=clone_cmddefsetup(self):ifnotos.path.isdir(self.test_repo_path):self.fetch_repo()deffetch_repo(self):""" Tries to fetch repository from remote path. """remote=self.remote_repoeprint("Fetching repository %s into %s"%(remote,self.test_repo_path))run_command(self.clone_cmd,'%s%s'%(remote,self.test_repo_path))defget_normalized_path(path):""" If given path exists, new path would be generated and returned. Otherwise same whats given is returned. Assumes that there would be no more than 10000 same named files. """ifos.path.exists(path):dir,basename=os.path.split(path)splitted_name=basename.split('.')iflen(splitted_name)>1:ext=splitted_name[-1]else:ext=Nonename='.'.join(splitted_name[:-1])matcher=re.compile(r'^.*-(\d{5})$')start=0m=matcher.match(name)ifnotm:# Haven't append number yet so return firstnewname='%s-00000'%namenewpath=os.path.join(dir,newname)ifext:newpath='.'.join((newpath,ext))returnget_normalized_path(newpath)else:start=int(m.group(1)[-5:])+1forxinxrange(start,10000):newname=name[:-5]+str(x).rjust(5,'0')newpath=os.path.join(dir,newname)ifext:newpath='.'.join((newpath,ext))ifnotos.path.exists(newpath):returnnewpathraiseVCSTestError("Couldn't compute new path for %s"%path)returnpath