This repository was archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Refactoring the disk cache to have directory as an init parameter #78
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -494,12 +494,12 @@ class Context(object): | |
| is passed to the context constructor. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The encoder.py file has grown to a size where I would consider splitting it up.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue #83 filed. |
||
| """ | ||
|
|
||
| def __init__(self, codec, cache_class=None): | ||
| def __init__(self, codec, cache_class=None, scoredir=None): | ||
| self.codec = codec | ||
| if cache_class: | ||
| self.cache = cache_class(self) | ||
| self.cache = cache_class(self, scoredir) | ||
| else: | ||
| self.cache = EncodingMemoryCache(self) | ||
| self.cache = EncodingMemoryCache(self, scoredir) | ||
|
|
||
|
|
||
| class Encoder(object): | ||
|
|
@@ -731,11 +731,16 @@ def _FileNameToVideofile(full_filename): | |
|
|
||
| class EncodingDiskCache(object): | ||
| """Encoder and encoding information, saved to disk.""" | ||
| def __init__(self, context): | ||
| def __init__(self, context, scoredir=None): | ||
| self.context = context | ||
| # Default work directory is current directory. | ||
| self.workdir = '%s/%s' % (encoder_configuration.conf.workdir(), | ||
| context.codec.name) | ||
| self.bad_encodings = {} | ||
| if scoredir: | ||
| self.workdir = os.path.join(encoder_configuration.conf.sysdir(), | ||
| scoredir, context.codec.name) | ||
| else: | ||
| # Default work directory. | ||
| self.workdir = os.path.join(encoder_configuration.conf.workdir(), | ||
| context.codec.name) | ||
| if not os.path.isdir(self.workdir): | ||
| os.mkdir(self.workdir) | ||
|
|
||
|
|
@@ -766,8 +771,13 @@ def _FilesToEncodings(self, files, videofile, bitrate=None, | |
| encoder or self._FileNameToEncoder(full_filename), | ||
| bitrate or _FileNameToBitrate(full_filename), | ||
| videofile or _FileNameToVideofile(full_filename)) | ||
| candidate.Recover() | ||
| candidates.append(candidate) | ||
| try: | ||
| candidate.Recover() | ||
| candidates.append(candidate) | ||
| except Error as err: | ||
| # TODO(hta): Change this to a more specific catch once available. | ||
| self.bad_encodings[full_filename] = err | ||
| continue | ||
| return candidates | ||
|
|
||
| def _QueryScoredEncodings(self, encoder=None, bitrate=None, videofile=None): | ||
|
|
@@ -848,22 +858,16 @@ def AllEncoderFilenames(self, only_workdir=False): | |
| def RemoveEncoder(self, hashname): | ||
| shutil.rmtree(os.path.join(self.workdir, hashname)) | ||
|
|
||
| def StoreEncoding(self, encoding, workdir=None): | ||
| def StoreEncoding(self, encoding): | ||
| """Stores an encoding object on disk. | ||
|
|
||
| An encoding object consists of its result (if executed). | ||
| The bitrate is encoded as a directory, the videofilename | ||
| is encoded as part of the output filename. | ||
| """ | ||
| if workdir: | ||
| dirname = os.path.join(workdir, | ||
| self.context.codec.name, | ||
| encoding.encoder.Hashname(), | ||
| self.context.codec.SpeedGroup(encoding.bitrate)) | ||
| else: | ||
| dirname = os.path.join(self.workdir, | ||
| encoding.encoder.Hashname(), | ||
| self.context.codec.SpeedGroup(encoding.bitrate)) | ||
| dirname = os.path.join(self.workdir, | ||
| encoding.encoder.Hashname(), | ||
| self.context.codec.SpeedGroup(encoding.bitrate)) | ||
| if not os.path.isdir(dirname): | ||
| os.mkdir(dirname) | ||
| if not encoding.result: | ||
|
|
@@ -872,17 +876,14 @@ def StoreEncoding(self, encoding, workdir=None): | |
| with open('%s/%s.result' % (dirname, videoname), 'w') as resultfile: | ||
| json.dump(encoding.result, resultfile, indent=2) | ||
|
|
||
| def ReadEncodingResult(self, encoding, scoredir=None): | ||
| def ReadEncodingResult(self, encoding): | ||
| """Reads an encoding result back from storage, if present. | ||
|
|
||
| None is returned if file does not exist. | ||
| If scoredir is given, only that directory is searched. | ||
| If it is not given, all directories in the search path are searched.""" | ||
|
|
||
| if scoredir: | ||
| searchpath = [os.path.join(scoredir, self.context.codec.name)] | ||
| else: | ||
| searchpath = self.SearchPathForScores() | ||
| searchpath = self.SearchPathForScores() | ||
|
|
||
| for workdir in searchpath: | ||
| dirname = os.path.join(workdir, encoding.encoder.Hashname(), | ||
|
|
@@ -903,13 +904,16 @@ def ReadEncodingResult(self, encoding, scoredir=None): | |
|
|
||
| class EncodingMemoryCache(object): | ||
| """Encoder and encoding information, in-memory only. For testing.""" | ||
| def __init__(self, context): | ||
| def __init__(self, context, scoredir=None): | ||
| if scoredir: | ||
| raise Error('Cannot have scoredir on a memory cache') | ||
| self.context = context | ||
| self.encoders = {} | ||
| self.encodings = [] | ||
| self.workdir = '/not-valid-file/' + self.context.codec.name | ||
|
|
||
| def WorkDir(self): | ||
| return '/not-valid-file/' + self.context.codec.name | ||
| return self.workdir | ||
|
|
||
| def AllScoredEncodings(self, bitrate, videofile): | ||
| result = [] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My standard comment: You are refactoring untested code. 2 risky aspects to the refactoring: 1. splicing in a new parameter between old ones. 2. Spitting up the role of my_optimizer into 2 objects, old_optimizer and new_optimizer.
Not sure if there is a good way to test this, but since the tests you wrote in the past so secure similar-sized changes actually caught bugs, I probably should at least raise this.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledged. The naming structure I've adopted makes it hard to write tests for code in binaries (no .py means that you can't "import" them into the test).
The bug that this fixes is that before, if a score got worsened, it would result in it disappearing from the list of possible best scores, which meant that the next run would test the change against a different score :-(
This was part of #82 - but I decided to separate the code work from the actual version flip.
My only mitigating point on the "insert parameter in the middle of list" is that the function is called from exactly one place, and because of the naming mentioned above, I'm assured that it's not imported anywhere else :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All fair enough. Fix for the hard-to-test binaries is of course to move test-worthy code to lib. Granted, doesn't help as a safety net even for that bin2lib refactoring but might provide a motivation going forward to be more strict than in the past about allowing actual work code to live in the binaries directly.