共计 7278 个字符,预计需要花费 19 分钟才能阅读完成。
INFO1112Assignment1 课业解析
留学
INFO1112Assignment1 课业解析
Moon
2019-09-24 16:33:16
INFO1112Assignment1 课业解析
题意:
写一个类似 linux 里 find 命令的简化版本 python 脚本
解析:
find 命令能够让我们在文件系统空间中搜索满足某些条件的文件比如某些特定名称(完全匹配)、名称与正则表达式模式匹配的文件(部分匹配)。还需要实现显示搜索文件的某些特征(比如特定日期后者读写权限等),python 可使用 os.walk 对目录进行遍历,还需对文件名和扩展名进行遍历,可将文件名和扩展名以列表呈现,查找时对列表进行遍历即可。当遇到无效的命令行参数时应进行错误处理。
涉及知识点:
python 编程,os.walk、re 模块
更多可加 薇 欣 讨论
薇 欣 号:Dui0772
pdf
INFO1112 2019 Assignment 1
Due Week 9, Monday 7th October at 11:59PM (Midnight)
This assignment is worth 10% of your overall grade for the course.
Assessment
The assignment will be marked with an automatic testing system on Ed. An
automatic mark will be given based on a percentage of tests passed (6%) and a
manual mark will be given for overall style, quality, readability, etc (2%). You are
expected to write your own tests and submit them with your code, and a mark will
be given based on coverage and manual inspection of your tests (2%).
Marks will be allocated for :
● basic directory traversal
● regular expression
● executing external commands
● argument parsing
● test coverage
● code layout, readability, overall quality, etc
Other behaviours will not be assessed.
This assignment involves writing a command to scan the file system looking for a
name that matches a given regular expression and then executing another
command. It is to be developed using the Python language. The specification for the
command is given below.
INFO1112 2019 Assignment 1 1
The Find Command
All Unix-like systems (Linux, etc) have a command called “find”. This command
allows you to search the file system name-space for a file that satisfies some criteria.
For example, you might search for a file with a particular name (exact match), or you
might search for a file with a name that matches a regular expression pattern. You
might search for a file that has certain characteristics such as created after a
particular date or has certain permissions (eg writable).
You can see the detailed specification of this command using the Unix man
command, eg ” man find “.
Assignment Specification
You are to implement a very simplified version of the Unix find command.
The syntax for your command is:
myfind.py [–regex=pattern | –name=filename] directory [command]
myfind.pyis the command name
directoryis a required argument for the path to the directory to traverse
filenameis the exact filename to match against when traversing the directory
patternis the regular expression pattern to match against when traversing the
directory
Only one of –regex=patternor –name=filenameshould be provided. If
neither are provided, then list all files found during the traversal. These flags may be
located anywhere in the argument list.
commandis an optional argument which is a Unix command that has {}in the
command string replaced by the file name that is currently being executed on. If this
argument is not provided, then just list the file paths of the discovered files. This
string should be a simple command with an argument list (e.g. no redirections or
pipes).
Error handling and assumptions
If invalid command line arguments are provided, your program should output the
following to standard error and exit with a non-zero exit code.
Usage: myfind.py [–regex=pattern | –name=filename] directory
[command]
INFO1112 2019 Assignment 1 2
If any of the child processes could not be started, your program should print the
following to standard error and exit with a non-zero exit code. An example is
provided in the last usage example.
Error: Unable to start process ‘<command>’
You may assume that flags are always provided in the form –flag=value.
Usage Examples
List all files in the home directory tree
$ myfind.py ~
Here is an example of a short Bash session using this command (note that the order
of the command output is not guaranteed):
$ ls ~
2019-calendar 2018-calendar INFO1112
$ ls -l ~/INFO1112
2019-lecture-1.pdf 2018-lecture-1.pdf
$ myfind.py ~
/home/bob
/home/bob/2019-calendar
/home/bob/2018-calendar
/home/bob/INFO1112
/home/bob/INFO1112/2019-lecture-1.pdf
/home/bob/INFO1112/2018-lecture-1.pdf
List files in the current directory tree that are named ‘config’
$ myfind.py . –name=config
List files in the ‘Code’ directory tree that end in ‘.sh’
$ myfind.py Code –regex=”\.sh$”
Run ‘wc’on files in the user ‘bob’s home directory tree
This will run a separate wcprocess for every file discovered while traversing the
directory.
$ myfind.py ~bob “wc {}”
Run ‘ls -l’on files in the home directory tree which start with ‘^2019’
This will run a separate lsprocess for every matching file discovered while
traversing the directory.
$ myfind.py ~ “^2019” “ls -l {}”
Here is an example of a short Bash session using this command (note that the order
of the command output is not guaranteed):
INFO1112 2019 Assignment 1 3
$ ls ~
2019-calendar 2018-calendar INFO1112
$ ls -l ~/INFO1112
2019-lecture-1.pdf 2018-lecture-1.pdf
$ myfind.py ~ –regex=”^2019″ “ls -l {}”
-rw-r–r–@ 1 bob 502 0 14 Sep 20:03 2019-calendar
-rw-r–r–@ 1 bob 502 0 14 Sep 20:04 INFO1112/2019-lecture-1.pdf
If a process could not be opened for the command for the calendar file, then the
output may be the following (the calendar line may come first):
-rw-r–r–@ 1 bob 502 0 14 Sep 20:04 INFO1112/2019-lecture-1.pdf
Error: Unable to start process ‘ls -l 2019-calender’
Please note, further clarifications to this specification may be posted on Ed.
Implementation
The assignment is to be implemented in Python as a script which handles command
line arguments. A scaffold file will be provided. You are expected to write legible
code following the PEP-8 style guide.
It is expected that your program uses sub-processes to handle the command
argument.
It is suggested that you use the os.walkfunction and the remodule.
You must implement the assignment without using os.systemor the subprocess
module.
The only Python modules which you are allowed to import are os, sys, and re. If
you want to use an additional module which will not trivialize the assignment, ask
your tutor, and the allowed library list may be extended. Argument parsing libraries
such as argparsewill not be permitted.
Testing
You are expected to write a number of test cases for your program. These should be
simple input/output tests. An example test will be included in the scaffold. You are
expected to test every execution path of your code.
You’re suggested to write a simple testing script in Bash to run your input/output
tests to simplify and automate the testing process.
INFO1112 2019 Assignment 1 4
Submitting your code
An Ed assessment workspace will be available soon for you to submit your code.
Public and hidden test cases will be released in batches until Sunday the 29th of
September. Additionally, there will be a set of unreleased test cases which will be
run against your code after the due date.
Any attempt to deceive the automatic marking system will be subject to academic
dishonesty proceedings.
Academic Declaration
By submitting this assignment you declare the following:
I declare that I have read and understood the University of Sydney Student
Plagiarism: Coursework Policy and Procedure, and except where specifically
acknowledged, the work contained in this assignment/project is my own work, and
has not been copied from other sources or been previously submitted for award or
assessment.
I understand that failure to comply with the Student Plagiarism: Coursework Policy
and Procedure can lead to severe penalties as outlined under Chapter 8 of the
University of Sydney By-Law 1999 (as amended). These penalties may be imposed
in cases where any significant portion of my submitted work has been copied without
proper acknowledgement from other sources, including published works, the
Internet, existing programs, the work of other students, or work previously submitted
for other awards or assessments.
I realise that I may be asked to identify those portions of the work contributed by me
and required to demonstrate my knowledge of the relevant material by answering
oral questions or by undertaking supplementary work, either written or in the
laboratory, in order to arrive at the final assessment mark.
I acknowledge that the School of Computer Science, in assessing this assignment,
may reproduce it entirely, may provide a copy to another member of faculty, and/or
communicate a copy of this assignment to a plagiarism checking service or in-house
computer program, and that a copy of the assignment may be maintained by the
service or the School of Computer Science for the purpose of future plagiarism
checking.
INFO1112 2019 Assignment 1 5