Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write configuration to a file in home directory #86

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
HISTFILE=.bash_history
set -o history
pip install .
hm-init bashrc --home `pwd`
. bashrc
history-manager-init --home `pwd`
. `pwd`/configuration
py.test
echo +++++++++++++ System tests
. tests/release/prepare_history.sh
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ For other installation options please follow the [link](docs/installation.md)

## Configuration

At the configuration step the history manager adds some wrapper scripts to the `.bashrc`:
At the configuration step the history manager generates configuration file with:

```Shell
hm-init ~/.bashrc
hm-init
```
To enable `hm` in the current session source the `.bashrc`:
To enable `hm` in the current session execute:

```Shell
. ~/.bashrc
. ~/.hm/configuration
```

And modify `~/.bashrc` to enable `hm` by default in future sessions:

```Shell
echo ". ~/.hm/configuration" >> ~/.bashrc
```

More details are [here](docs/configuration.md).
Expand Down
42 changes: 21 additions & 21 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@

## Configuration

### Adding `hm` scripts to the Bash configuration
### Adding `hm` scripts to the Bash configuration (python package)

Configuration of the history manager is performed by the following command:
Configuration of the history manager is generated with python script when installed with `pip`:

```Shell
hm-init
```
or for the installation from sources:
To enable `hm` in the current session execute:

```Shell
hm-db configure
. ~/.hm/configuration
```
This command will output everything to the terminal. To add the content to `~/.bashrc` run:

```Shell
hm-init ~/.bashrc
```
or
And modify `~/.bashrc` to enable `hm` by default in future sessions:

```Shell
hm-db configure ~/.bashrc
echo ". ~/.hm/configuration" >> ~/.bashrc
```

If it's not convenient to use `~/.bashrc` directly the configuration scripts can be placed to a separate file `~/.hm/configuration`:
### Adding `hm` scripts to the Bash configuration (installed from sources)

Call `hm-db` directly:

```Shell
hm-init ~/.hm/configuration
hm-db configure
```
and add the following line to the `~/.bashrc`:
which will output everything to the terminal or to a specified file, for example:

```Shell
[[ -f ~/.hm/configuration ]] && . ~/.hm/configuration
hm-db configure ~/.bashrc
```

### Directory to store `hm` database
If it's not convenient to use `~/.bashrc` directly the configuration scripts can be placed to an arbitrary file:

History manager home directory is `~/.hm` by default. Other home path can be specified at configuration step:
```Shell
hm-db configure <filename>
```
and add the following line to the `~/.bashrc`:

```Shell
hm-init ~/.bashrc --home /custom/hm/home/path
. <filename>
```

### Applying changes to the current session
### Home directory for history manager

Simply source the `.bashrc`:
By default it is `~/.hm`. Other home path can be specified at configuration step:

```Shell
. ~/.bashrc
hm-init --home /custom/hm/home/path
```

39 changes: 24 additions & 15 deletions hm_initializer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
import subprocess
import argparse

HM_HOME=os.path.expanduser("~/.hm")

def main():
parser = argparse.ArgumentParser(description='Python wrapper for history manager configurator')
parser.add_argument('--home', nargs='?', default=HM_HOME, help='Path to store database of history manager')
args = parser.parse_args()

config_filename = os.path.join(args.home, "configuration")

# Print a link to history manager configuration
configuration_tips = "\nTo enable history manager (hm) in the current session execute:\n"
configuration_tips += "\n\t. {}\n".format(config_filename);
configuration_tips += "\nand modify ~/.bashrc to enable it by default in future sessions:\n"
configuration_tips += "\n\techo \". {}\" >> ~/.bashrc\n".format(config_filename);
print(configuration_tips)

# Create history manager home directory (command database will be placed there)
if not os.path.exists(args.home):
os.makedirs(args.home)

# Initialize configuration file with PATH environment variable modification
cwd = os.path.abspath(os.path.dirname(__file__))
hm_db_path = os.path.abspath(os.path.join(cwd, os.pardir))
path_modification = "PATH=$PATH:" + hm_db_path + "\n"

parser = argparse.ArgumentParser(description='Python wrapper for history manager configurator')
parser.add_argument('dest', nargs='?', help='A destination file to write history manager\'s configuration')
parser.add_argument('--home', nargs='?', help='Path to store database of history manager')
args = parser.parse_args()

if args.dest != None:
with open(args.dest, 'a') as f:
f.write(path_modification)
else:
print(path_modification)

cmd = ' '.join([hm_db_path + '/hm-db', 'configure'] + sys.argv[1:])
#print(cmd)
return_code = subprocess.call(cmd, shell=True)
with open(config_filename, 'w') as f:
f.write(path_modification)

#print("hm should be initialized here")
# Call configuration procedure od hm-db binary
cmd = ' '.join([hm_db_path + '/hm-db', 'configure', config_filename, '--home', args.home])
return_code = subprocess.call(cmd, shell=True)
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
with open(path.join(cwd, 'docs', 'python_package.md'), encoding='utf-8') as f:
long_description = f.read()

class CMakeExtension(Extension):

class CMakeExtension(Extension):
def __init__(self, name):
# don't invoke the original build_ext for this special extension
super().__init__(name, sources=[])


Expand All @@ -29,7 +28,6 @@ def build_cmake(self, ext):
par_ext_dir = path.abspath(path.join(ext_dir, pardir))
makedirs(ext_dir, exist_ok=True)

#config = 'Debug' if self.debug else 'Release'
cmake_args = [
'-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=' + par_ext_dir,
]
Expand All @@ -43,8 +41,6 @@ def build_cmake(self, ext):
if not self.dry_run:
self.spawn(['cmake', '--build', '.'] + build_args)

print(self.build_temp)

chdir(cwd)


Expand All @@ -63,7 +59,7 @@ def build_cmake(self, ext):
'build_ext': build_ext,
},
entry_points = {
'console_scripts': ['hm-init=hm_initializer:main'],
'console_scripts': ['history-manager-init=hm_initializer:main'],
},
zip_safe=False)

Expand Down
4 changes: 2 additions & 2 deletions src/history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ class History {

void select(bool by_dir, const std::string& path, bool recursively,
bool by_sess, const std::string& sess) {
std::string select_sql = prepare_select((by_dir) ? path : "", recursively,
(by_sess) ? sess : "");
const std::string select_sql = prepare_select(
(by_dir) ? path : "", recursively, (by_sess) ? sess : "");
sqlite::Query query(db, select_sql);

while (query.exec_step()) {
Expand Down