def download(self, dest, is_skip_same=True): """Download file to dest. """ _dest = Path(dest) if not _dest.exists(): copy(self, _dest) return dest_hash = get_weak_filehash(_dest) if get_weak_filehash(self) != dest_hash: history_folder = Path(_dest.parent / self.history_dirname) history_folder.mkdir(parents=True, exist_ok=True) backup_file = (history_folder / _dest.with_suffix('.{}{}'.format( dest_hash[:8], _dest.suffix)).name) try: _dest.rename(backup_file) except OSError as ex: if ex.errno != errno.EEXIST: raise ex elif is_skip_same: return copy(self, _dest)
def compare_with(self, local_dir): """Check if file already downloaded to @local_dir. """ _local_dir = Path(local_dir) is_dir_exists = _local_dir.exists() existed = [get_weak_filehash(i) for i in _local_dir.iterdir()] for i in progress(list(self), '比较文件修改日期'): assert isinstance(i, RemoteFile) i.is_updated = is_dir_exists and get_weak_filehash(i) in existed
def get_weak_filehash(path): """Fast weak hash. """ h = hashlib.sha1() _path = Path(path) stat = _path.stat() h.update('{0:s}\n\n{1:.4f}\n\n{2:d}'.format(_path.name, stat.st_mtime, stat.st_size)) return h.hexdigest()
def _check(frame): try: path = Path(self.filename.with_frame(frame)) if path in checked: return if not path.is_file(): ret.add([frame]) checked.add(path) except OSError as ex: LOGGER.error(os.strerror(ex.errno), exc_info=True)
def import_video(self, sign): """Import corresponse video by filebox sign. Args: sign (unicode): Server defined fileboxsign Returns: nuke.Node: Created read node. """ node_name = {'animation_videos': '动画视频'}.get(sign, sign) n = nuke.toNode(utf8(node_name)) if n is None: dir_ = self.filebox.get(sign).path videos = Path(dir_).glob('{}.*'.format(self.shot)) for video in videos: n = nuke.nodes.Read(name=utf8(node_name)) n['file'].fromUserText(unicode(video).encode('utf-8')) break if not n: raise ValueError('No matched upstream video.') n['frame_mode'].setValue(b'start_at') n['frame'].setValue(b'{:.0f}'.format( nuke.numvalue('root.first_frame'))) CurrentViewer().link(n, 4, replace=False) return n
def test_from_path(self): first = FrameRanges('test测试') self.assertEqual(str(first), '1-1') last = FrameRanges('test测试.%04d.exr') root = FrameRanges.from_root() self.assertEqual(str(last), str(root)) last = FrameRanges(Path('test测试.%04d.exr')) self.assertEqual(str(last), str(root))
def split(filename, output_dir, file_format=None): """Render splited files. """ path = PurePath(re.sub(r' [\d -]*$', '', filename)) output_dir = Path(output_dir) # Create read node. read_node = nuke.nodes.Read() read_node['file'].fromUserText(filename) if file_format: assert isinstance(file_format, (str, unicode)) suffix = '.{}'.format(file_format.strip('.')) else: suffix = path.suffix or '.mov' # Get layers for render. layers = nuke.layers(read_node) assert isinstance(layers, list) layers_overlap = {'rgba': ('rgb', 'alpha')} for k, v in layers_overlap.items(): if k in layers: for i in v: try: layers.remove(i) except ValueError: pass # Create write nodes. for layer in layers: _kwargs = {'in': layer} # Avoid use of python keyword 'in'. n = nuke.nodes.Shuffle(inputs=[read_node], label=layer, **_kwargs) n = nuke.nodes.Write( inputs=[n], file=((output_dir / '{}.{}{}'.format(path.stem, layer, suffix)).as_posix()), channels='rgba') # Render. output_dir.mkdir(parents=True, exist_ok=True) nuke.render(nuke.Root(), start=read_node.firstFrame(), end=read_node.lastFrame())
def download(self): """Download Files. """ if self.is_downloading: return self.is_downloading = True self.buttonBox.setEnabled(False) try: is_skip_same = self.checkBoxSkipSame.isChecked() pool = Pool() for i in progress(self.files, '下载文件', parent=self): result = pool.apply_async(i.download, (Path(self.dir) / Path(i).name, ), dict(is_skip_same=is_skip_same)) while not result.ready(): QCoreApplication.processEvents() if not result.successful(): raise RuntimeError('Download failed', i) finally: self.is_downloading = False self.buttonBox.setEnabled(True)
def __init__(self, target=SUBMIT_FILE): select = cgtwq.DesktopClient().selection() files = set() if target == SUBMIT_FILE: # Get from submit files. files.update(select.flow.list_submit_file()) else: # Get from filebox. checked = set() for entry in progress(select.to_entries(), '获取文件框内容'): assert isinstance(entry, cgtwq.Entry) filebox = entry.filebox.from_id(target.id) path = Path(filebox.path) for rule in filebox.rule: key = (path, rule) if key in checked: continue files.update(i for i in path.glob(rule) if i.is_file()) checked.add(key) files = (RemoteFile(i) if not isinstance(i, RemoteFile) else i for i in list(files)) super(RemoteFiles, self).__init__(files)
def setUp(self): self.temp_dir = mkdtemp() self.addCleanup(os.removedirs, self.temp_dir) self.test_file = os.path.join(self.temp_dir, 'test_seq.%04d.exr') self.expected_missing_frames = nuke.FrameRanges( sample(xrange(1, 91), 10) + list(xrange(91, 101))) self.expected_missing_frames.compact() # Create temp file. path = Path(Path(self.temp_dir) / 'test_seq.%04d.exr') for i in set(xrange(1, 91)).difference( self.expected_missing_frames.toFrameList()): j = Path(path.with_frame(i)) with j.open('w') as f: f.write(unicode(i)) self.addCleanup(j.unlink)
def filename_factory(cls, obj): """get filename from a object. Args: obj (str or unicode or nuke.Node): Object contain filename. Returns: wlf.path.Path: filename path. """ filename = obj if isinstance(obj, nuke.Node): filename = nuke.filename(obj) elif isinstance(obj, Footage): filename = obj.filename elif isinstance(obj, (str, unicode)): pass else: raise TypeError('can not use as filename: {}'.format(type(obj))) path = Path(u(filename)) return path